﻿Partial Public Class RelationMeterial
    Inherits System.Web.UI.Page

    Dim clsGet As New ClassFunctionGetData
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            ViewState("dtCustomer") = createCustomerDataTable()
            'Dim lbMenu As Label = CType(Master.FindControl("lbMenu"), Label)
            'lbMenu.Text = "Material"
            If (Session("EditMaster") Is Nothing) Then
                btnSave.Visible = False
            End If

            ViewState("Product") = CreateDatasetProduct()
            ViewState("Relation") = CreateDataset()
            If Not (Session("MatRelation") Is Nothing) Then
                lbCode.Text = Session("MatRelation").ToString().Split("$")(0)
                lbName.Text = Session("MatRelation").ToString().Split("$")(1)
                Dim ds As DataSet = clsGet.Get_MeterailMasterForEditPopup(lbCode.Text)  'Dataset ข้อมูล
                If (ds.Tables(0).Rows.Count > 0) Then
                    ' If Not (ds.Tables(0).Rows(0).Item("MAT_Default") & "" = "") Then
                    If Not (ddlPer.Items.FindByValue(ds.Tables(0).Rows(0).Item("UNIT") & "") Is Nothing) Then
                        ddlPer.ClearSelection()
                        ddlPer.SelectedValue = ds.Tables(0).Rows(0).Item("UNIT") & ""
                    End If
                    If Not (ddlWHT.Items.FindByValue(ds.Tables(0).Rows(0).Item("MAT_WHT") & "") Is Nothing) Then
                        ddlWHT.ClearSelection()
                        ddlWHT.SelectedValue = ds.Tables(0).Rows(0).Item("MAT_WHT") & ""
                    End If
                    If (ds.Tables(0).Rows(0).Item("MAT_Shifting") & "" <> "") Then
                        txtShifting.Text = ds.Tables(0).Rows(0).Item("MAT_Shifting") & ""
                    End If
                    If (ds.Tables(0).Rows(0).Item("MAT_DWToperate") & "" <> "") Then
                        txtDWToperate.Text = ds.Tables(0).Rows(0).Item("MAT_DWToperate") & ""
                    End If
                    If Not (ddlOperate.Items.FindByValue(ds.Tables(0).Rows(0).Item("MAT_Operate") & "") Is Nothing) Then
                        ddlOperate.SelectedValue = ds.Tables(0).Rows(0).Item("MAT_Operate") & ""
                    End If
                    If (ds.Tables(0).Rows(0).Item("MAT_Default") & "" = "Y") Then
                        If (ds.Tables(0).Rows(0).Item("MAT_PORT") & "" <> "") Then
                            If (ds.Tables(0).Rows(0).Item("MAT_PORT") & "" = "A") Then
                                For Each lt As ListItem In chkDefault.Items
                                    lt.Selected = True
                                Next
                            Else
                                For Each lt As ListItem In chkDefault.Items
                                    If (ds.Tables(0).Rows(0).Item("MAT_PORT") & "" = lt.Value) Then
                                        lt.Selected = True
                                    End If
                                Next
                            End If
                        End If
                    End If

                    If Not (radioCompute.Items.FindByValue(ds.Tables(0).Rows(0).Item("MAT_Compute") & "") Is Nothing) Then
                        radioCompute.SelectedValue = ds.Tables(0).Rows(0).Item("MAT_Compute")
                    End If

                    radio_DestionationCompute.SelectedValue = ds.Tables(0).Rows(0).Item("MAT_ComputeDestinaation")
                    If (radio_DestionationCompute.SelectedValue = "O") Then
                        rowTypebuyCompute.Visible = True
                        radioTypeBuyCompute.SelectedValue = ds.Tables(0).Rows(0).Item("MAT_ComputeBuy")
                    End If
                    If Not (radioNationalCompute.Items.FindByValue(ds.Tables(0).Rows(0).Item("MAT_ComputeNational") & "") Is Nothing) Then
                        radioNationalCompute.SelectedValue = ds.Tables(0).Rows(0).Item("MAT_ComputeNational") & ""
                    End If
                    If Not (radioBuyerSeller.Items.FindByValue(ds.Tables(0).Rows(0).Item("MAT_ComputebuyerSeller") & "") Is Nothing) Then
                        radioBuyerSeller.SelectedValue = ds.Tables(0).Rows(0).Item("MAT_ComputebuyerSeller") & ""
                    End If

                    If Not (radioComputeProduct.Items.FindByValue(ds.Tables(0).Rows(0).Item("MAT_ComputeProduct") & "") Is Nothing) Then
                        radioComputeProduct.SelectedValue = ds.Tables(0).Rows(0).Item("MAT_ComputeProduct") & ""
                    End If

                    If (radioComputeProduct.SelectedValue = "A") Then

                    ElseIf (radioComputeProduct.SelectedValue = "B") Then
                        rowComputeProduct1.Visible = True
                        Dim dtpd As DataTable = clsGet.Get_Mat_PRoduct(lbCode.Text, "C")
                        If dtpd.Rows.Count = 0 Then
                            ViewState("ComputeProduct") = CreateDatasetProduct()
                        Else
                            ViewState("ComputeProduct") = dtpd
                        End If
                        gridComputeProduct1.PageIndex = 0
                        gridComputeProduct1.DataSource = ViewState("ComputeProduct")
                        gridComputeProduct1.DataBind()
                    ElseIf (radioComputeProduct.SelectedValue = "C") Then
                        rowComputeProduct2.Visible = True
                        Dim dtpd As DataTable = clsGet.Get_Mat_PRoduct(lbCode.Text, "C")
                        If dtpd.Rows.Count = 0 Then
                            ViewState("ComputeProduct") = CreateDatasetProduct()
                        Else
                            ViewState("ComputeProduct") = dtpd
                        End If
                        gridComputeProduct2.PageIndex = 0
                        gridComputeProduct2.DataSource = ViewState("ComputeProduct")
                        gridComputeProduct2.DataBind()
                    End If
                    If Not (radioFor.Items.FindByValue(ds.Tables(0).Rows(0).Item("MAT_FOR") & "") Is Nothing) Then
                        radioFor.SelectedValue = ds.Tables(0).Rows(0).Item("MAT_FOR") & ""
                    End If
                    Dim unitcalText As String = ""
                    Dim unitcaltText As String = ""
                    Dim test As String = ""
                    For Each dr As DataRow In ds.Tables(0).Rows
                        Select Case (dr.Item("Relation") & "")
                            Case "01" : unitcalText = "GRT"
                            Case "02" : unitcalText = "L.O.A."
                            Case "03" : unitcalText = "DWT"
                            Case "04" : unitcalText = "Draft"
                            Case "05" : unitcalText = "Product"
                        End Select
                        AddRelation(dr.Item("MAT_Name").ToString() & "", dr.Item("Point_Start").ToString() & "", dr.Item("Point_End").ToString() & "", dr.Item("Mat_Rate").ToString() & "", dr.Item("Money").ToString() & "", dr.Item("Money").ToString() & "", dr.Item("Relation").ToString() & "", unitcalText, "", dr.Item("Point_Unit") & "", dr.Item("Vessel_national") & "", dr.Item("Vessel_Destination") & "", dr.Item("Data3") & "", dr.Item("Compute_Between") & "", dr.Item("Customer_no") & "", dr.Item("Customer_name") & "", dr.Item("Customer_Rate") & "", dr.Item("Customer_Money") & "", dr.Item("Compute_Count") & "", dr.Item("LowRate") & "", dr.Item("LowRate_Money") & "", dr.Item("Compute_C") & "")
                    Next
                    'End If

                End If
            End If
            gridItem.DataSource = ViewState("Relation")
            gridItem.DataBind()
        End If
    End Sub

    Protected Sub btnclose_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnclose.Click
        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " parent.closeModal();", True)
    End Sub

    Public Function CreateDataset() As DataSet
        Dim ds As New DataSet
        Dim dt As New DataTable
        Dim dr As DataRow
        ds.Tables.Add(dt)
        dt.Columns.Add("Name", GetType(String))
        dt.Columns.Add("Point", GetType(String))
        dt.Columns.Add("Rate", GetType(String))
        dt.Columns.Add("Unit", GetType(String))
        dt.Columns.Add("Mat_Unit", GetType(String))
        dt.Columns.Add("Mat_Rate", GetType(String))
        dt.Columns.Add("Relation", GetType(String))
        dt.Columns.Add("Point_Start", GetType(String))
        dt.Columns.Add("Point_End", GetType(String))
        dt.Columns.Add("cal_point", GetType(String))
        dt.Columns.Add("Money", GetType(String))
        dt.Columns.Add("Point_Unit", GetType(String))
        dt.Columns.Add("Vessel_national", GetType(String))
        dt.Columns.Add("Vessel_Destination", GetType(String))
        dt.Columns.Add("Data3", GetType(String))
        dt.Columns.Add("Compute_Between", GetType(String))
        dt.Columns.Add("Customer_no", GetType(String))
        dt.Columns.Add("Customer_name", GetType(String))
        dt.Columns.Add("Customer_Rate", GetType(String))
        dt.Columns.Add("Customer_Money", GetType(String))
        dt.Columns.Add("Compute_Count", GetType(String))
        dt.Columns.Add("LowRate", GetType(String))
        dt.Columns.Add("LowRate_Money", GetType(String))
        dt.Columns.Add("Compute_C", GetType(String))
        ds.Tables(0).AcceptChanges()
        dr = ds.Tables(0).NewRow
        dr("name") = ""
        dr("Point") = ""
        dr("Rate") = ""
        dr("Unit") = ""
        dr("Mat_Unit") = ""
        dr("Mat_Rate") = ""
        dr("Relation") = ""
        dr("Point_Start") = ""
        dr("Point_End") = ""
        dr("cal_point") = ""
        dr("Money") = ""
        dr("Point_Unit") = ""
        dr("Vessel_national") = ""
        dr("Vessel_Destination") = ""
        dr("Data3") = ""
        dr("Compute_Between") = ""
        dr("Customer_no") = ""
        dr("Customer_name") = ""
        dr("Customer_Rate") = ""
        dr("Customer_Money") = ""
        dr("Compute_Count") = ""
        dr("LowRate") = ""
        dr("LowRate_Money") = ""
        dr("Compute_C") = ""
        ds.Tables(0).Rows.Add(dr)
        Return ds
    End Function

    Private Sub btnClear_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClear.Click
        txtRate.Text = String.Empty
        txtStart.Text = String.Empty
        txtEnd.Text = String.Empty
        txtNameCal.Text = String.Empty
        txtPer.Text = String.Empty
        ViewState("dtCustomer") = createCustomerDataTable()
        gridCustomer.DataSource = ViewState("dtCustomer")
        gridCustomer.DataBind()
        ddlUnitCal.ClearSelection()
        ddlRate.ClearSelection()
        ViewState("editcal") = ""
        clearTextRelation()
    End Sub

    Protected Sub btnRelation_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnRelation.Click
        If (radioComputeBettween.SelectedValue = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('กรุณาทำการระบุวิธีการคำนวณ !!');", True)
            Exit Sub
        End If
        If (RadioRate.Checked) Then
            If (txtRate.Text.Trim = "") Or (txtRate.Text.Trim() = ".") Then
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('กรุณาทำการระบุ Rate !!');", True)
                Exit Sub
            End If
        End If
        If (txtLowRate.Text.Trim = "") Or (txtLowRate.Text.Trim() = ".") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('กรุณาทำการระบุราคาขั้นต่ำ !!');", True)
            Exit Sub
        End If
        If (txtPer.Text.Trim() = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('กรุณาทำการระบุจำนวนหน่วย !!');", True)
            Exit Sub
        End If
        If (txtStart.Text.Trim() = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('กรุณาทำการระบุจำนวนเริ่มต้น !!');", True)
            Exit Sub
        End If
        If (txtEnd.Text.Trim() = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('กรุณาทำการระบุจำนวนสิ้นสุด !!');", True)
            Exit Sub
        End If
        Dim CountFix As String = ""
        If (radioData3.SelectedValue = "C") Then
            If (radioCount.SelectedValue = "F") Then
                If (txtCountFix.Text.Trim = "") Then
                    ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('กรุณาทำการระบุค่าคงที่ของ Count !!');", True)
                    Exit Sub
                Else
                    CountFix = txtCountFix.Text
                End If
            End If
        End If
        If Not (txtNameCal.Text.Trim() = "") Then
            Dim Customer_no As String = ""
            Dim Customer_name As String = ""
            Dim Customer_Rate As String = ""
            Dim Customer_Money As String = ""
            Dim dt As DataTable = ViewState("dtCustomer")
            For Each dr As DataRow In dt.Rows
                If (dr.Item("Customer_no") & "" <> "") Then
                    Customer_no &= "," & dr.Item("Customer_no")
                    Customer_name &= "," & dr.Item("Customer_name")
                    Customer_Rate &= "," & dr.Item("Customer_Rate")
                    Customer_Money &= "," & dr.Item("Customer_Money")
                End If
            Next
            If (Customer_no.Length > 0) Then
                Customer_no = Customer_no.Substring(1, Customer_no.Length - 1)
                Customer_name = Customer_name.Substring(1, Customer_name.Length - 1)
                Customer_Rate = Customer_Rate.Substring(1, Customer_Rate.Length - 1)
                Customer_Money = Customer_Money.Substring(1, Customer_Money.Length - 1)
            End If
            If (ViewState("editcal") & "" = "") Then
                Dim Rate As String = ""
                Dim UnitValue As String = ""
                Dim UnitText As String = ""
                Dim LowRate As String = txtLowRate.Text
                Dim LowRate_Money As String = ddlLowRate.SelectedItem.Text
                If (RadioRate.Checked) Then
                    Rate = CDbl(txtRate.Text).ToString("######0.00")
                    UnitText = ddlRate.SelectedItem.Text
                    UnitValue = ddlRate.SelectedValue
                Else
                    Rate = "((LOA-5)*Draft)+10%"
                    UnitText = ""
                    UnitValue = ""
                End If
                AddRelation(txtNameCal.Text, txtStart.Text, txtEnd.Text, Rate, UnitValue, UnitText, ddlUnitCal.SelectedValue, ddlUnitCal.SelectedItem.Text, "", txtPer.Text, radioNation.SelectedValue, radioDestination.SelectedValue, radioData3.SelectedValue, radioComputeBettween.SelectedValue, Customer_no, Customer_name, Customer_Rate, Customer_Money, CountFix, LowRate, LowRate_Money, radioCount.SelectedValue)
            Else
                Dim index As Integer = ViewState("editcal")
                Dim ds As DataSet = ViewState("Relation")
                ds.Tables(0).Rows(index).Item("Name") = txtNameCal.Text
                ds.Tables(0).Rows(index).Item("Point") = txtStart.Text & " - " & txtEnd.Text
                ds.Tables(0).Rows(index).Item("Unit") = ddlUnitCal.SelectedItem.Text
                If (RadioRate.Checked) Then
                    ds.Tables(0).Rows(index).Item("Rate") = CDbl(txtRate.Text).ToString("######0.00") & " " & ddlRate.SelectedItem.Text
                    ds.Tables(0).Rows(index).Item("Mat_Rate") = CDbl(txtRate.Text).ToString("#########0.00")
                Else
                    ds.Tables(0).Rows(index).Item("Rate") = "((LOA-5)*Draft) + 10%"
                    ds.Tables(0).Rows(index).Item("Mat_Rate") = "((LOA-5)*Draft) + 10%"

                End If
                ds.Tables(0).Rows(index).Item("LowRate") = txtLowRate.Text
                ds.Tables(0).Rows(index).Item("LowRate_Money") = ddlLowRate.SelectedItem.Text
                ds.Tables(0).Rows(index).Item("Relation") = ddlUnitCal.SelectedValue
                ds.Tables(0).Rows(index).Item("Point_Start") = txtStart.Text
                ds.Tables(0).Rows(index).Item("Point_End") = txtEnd.Text
                ds.Tables(0).Rows(index).Item("cal_point") = ""
                ds.Tables(0).Rows(index).Item("Money") = ddlRate.SelectedValue
                ds.Tables(0).Rows(index).Item("Point_Unit") = txtPer.Text
                ds.Tables(0).Rows(index).Item("Vessel_national") = radioNation.SelectedValue
                ds.Tables(0).Rows(index).Item("Vessel_Destination") = radioDestination.SelectedValue
                ds.Tables(0).Rows(index).Item("Data3") = radioData3.SelectedValue
                ds.Tables(0).Rows(index).Item("Compute_Between") = radioComputeBettween.SelectedValue
                ds.Tables(0).Rows(index).Item("Customer_no") = Customer_no
                ds.Tables(0).Rows(index).Item("Customer_name") = Customer_name
                ds.Tables(0).Rows(index).Item("Customer_Rate") = Customer_Rate
                ds.Tables(0).Rows(index).Item("Customer_Money") = Customer_Money
                ds.Tables(0).Rows(index).Item("Compute_Count") = CountFix
                ds.Tables(0).Rows(index).Item("Compute_C") = radioCount.SelectedValue
                ds.Tables(0).AcceptChanges()
                ViewState("editcal") = ds
                clearTextRelation()
            End If
            ViewState("dtCustomer") = createCustomerDataTable()
            gridCustomer.DataSource = ViewState("dtCustomer")
            gridCustomer.DataBind()
            ViewState("editcal") = ""
            gridItem.DataSource = ViewState("Relation")
            gridItem.DataBind()
        End If
    End Sub

    Private Sub AddRelation(ByVal t_NameCal As String, ByVal t_start As String, ByVal t_end As String, ByVal t_rate As String, ByVal d_rate As String, ByVal dt_rate As String, ByVal d_unitcal As String, ByVal t_unitcal As String, ByVal cal As String, ByVal Per As String, ByVal Nation As String, ByVal Destination As String, ByVal Data3 As String, ByVal Compute_Between As String, ByVal Customer_no As String, ByVal Customer_name As String, ByVal Customer_Rate As String, ByVal Customer_Money As String, ByVal Compute_Count As String, ByVal LowRate As String, ByVal LowRate_Money As String, ByVal Compute_C As String)
        Dim Ds As DataSet = ViewState("Relation")
        If (Ds.Tables(0).Rows(0).Item("Name") = "") Then
            Ds.Tables(0).Rows(0).Item("Name") = t_NameCal
            Ds.Tables(0).Rows(0).Item("Point") = t_start & " - " & t_end
            Ds.Tables(0).Rows(0).Item("Rate") = t_rate & " " & dt_rate
            Ds.Tables(0).Rows(0).Item("Unit") = t_unitcal

            ' Ds.Tables(0).Rows(0).Item("Mat_Unit") = ddlUnitCal.SelectedValue
            Ds.Tables(0).Rows(0).Item("Mat_Rate") = t_rate
            Ds.Tables(0).Rows(0).Item("Relation") = d_unitcal
            Ds.Tables(0).Rows(0).Item("Point_Start") = t_start
            Ds.Tables(0).Rows(0).Item("Point_End") = t_end
            Ds.Tables(0).Rows(0).Item("cal_point") = cal
            Ds.Tables(0).Rows(0).Item("Money") = d_rate
            Ds.Tables(0).Rows(0).Item("Point_Unit") = Per
            Ds.Tables(0).Rows(0).Item("Vessel_national") = Nation
            Ds.Tables(0).Rows(0).Item("Vessel_Destination") = Destination
            Ds.Tables(0).Rows(0).Item("Data3") = Data3
            Ds.Tables(0).Rows(0).Item("Compute_Between") = Compute_Between
            Ds.Tables(0).Rows(0).Item("Customer_no") = Customer_no
            Ds.Tables(0).Rows(0).Item("Customer_name") = Customer_name
            Ds.Tables(0).Rows(0).Item("Customer_Rate") = Customer_Rate
            Ds.Tables(0).Rows(0).Item("Customer_Money") = Customer_Money
            Ds.Tables(0).Rows(0).Item("Compute_Count") = Compute_Count
            Ds.Tables(0).Rows(0).Item("LowRate") = LowRate
            Ds.Tables(0).Rows(0).Item("LowRate_Money") = LowRate_Money
            Ds.Tables(0).Rows(0).Item("Compute_C") = Compute_C
            Ds.Tables(0).AcceptChanges()
        Else
            Dim dr As DataRow = Ds.Tables(0).NewRow
            dr.Item("Name") = t_NameCal
            dr.Item("Point") = t_start & " - " & t_end
            dr.Item("Rate") = t_rate & " " & dt_rate
            dr.Item("Unit") = t_unitcal

            ' dr("Mat_Unit") = ddlUnitCal.SelectedValue
            dr("Mat_Rate") = t_rate
            dr("Relation") = d_unitcal
            dr("Point_Start") = t_start
            dr("Point_End") = t_end
            dr("cal_point") = cal
            dr("Money") = d_rate
            dr("Point_Unit") = Per
            dr("Vessel_national") = Nation
            dr("Vessel_Destination") = Destination
            dr("Data3") = Data3
            dr("Compute_Between") = Compute_Between
            dr("Customer_no") = Customer_no
            dr("Customer_name") = Customer_name
            dr("Customer_Rate") = Customer_Rate
            dr("Customer_Money") = Customer_Money
            dr("Compute_Count") = Compute_Count
            dr("LowRate") = LowRate
            dr("LowRate_Money") = LowRate_Money
            dr("Compute_C") = Compute_C
            Ds.Tables(0).Rows.Add(dr)
            Ds.Tables(0).AcceptChanges()
        End If
        clearTextRelation()
        ViewState("Relation") = Ds
    End Sub

    Private Sub clearTextRelation()
        txtNameCal.Text = String.Empty
        txtStart.Text = String.Empty
        txtEnd.Text = String.Empty
        txtRate.Text = String.Empty
        txtPer.Text = String.Empty
        txtid.Text = String.Empty
        txtCustomerRate.Text = String.Empty
        ddlRate.ClearSelection()
        ddlUnitCal.ClearSelection()
        radioData3.ClearSelection()
        radioComputeBettween.ClearSelection()
        radioComputeBettween.SelectedValue = "A"
        radioData3.SelectedValue = "N"
        rowCount.Visible = False
        txtCountFix.Text = String.Empty
        radioDestination.ClearSelection()
        radioDestination.SelectedValue = "A"
        radioNation.ClearSelection()
        radioNation.SelectedValue = "A"
        RadioRate.Checked = True
        RadioLOA.Checked = False
        txtRate.Enabled = True
        ddlRate.Enabled = True
        txtLowRate.Text = String.Empty
        ddlLowRate.ClearSelection()
    End Sub

    Private Function CheckParameter() As Boolean
        If (radioCompute.SelectedValue = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('กรุณาทำการเลือกวิธีการคำนวณเงิน !!');", True)
        End If
        If (radioCompute.SelectedValue = "B") Then
            If (radio_DestionationCompute.SelectedValue = "") Then
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('กรุณาทำการเลือก Destination ที่จะทำการคำนวณเงิน !!');", True)
            End If
            If (radio_DestionationCompute.SelectedValue = "O") Then
                If (radioTypeBuyCompute.SelectedValue = "") Then
                    ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('กรุณาทำการเลือกรูปแบบการซื้อขาย ที่จะทำการคำนวณเงิน !!');", True)
                End If
            End If
        End If
        If (radioFor.SelectedValue = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('กรุณาทำการเลือกผู้ที่จะทำการจ่ายเงิน !!');", True)
        End If
    End Function

    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click

        If (CheckParameter()) Then
            Exit Sub
        End If
        Dim conn As New System.Data.SqlClient.SqlConnection(connString)
        Dim clsDB As New ClassDatabase
        Dim clsFc As New ClassFunction
        Dim condition As Boolean = True
        Dim tran As System.Data.SqlClient.SqlTransaction = Nothing
        Application.Lock()
        conn.Open()
        Dim MAT_CODE As String = clsDB.GeneratorID(conn, "RelationMeterial")
        Dim dt As DataTable = ViewState("Product")
        Dim dc As DataTable = ViewState("ComputeProduct")

        Dim MAT_FOR As String = ""
        Dim MAT_Relation As String = ""

        Dim DefaultTariff As String = "N"
        Dim PORT As String = ""
        For Each lt As ListItem In chkDefault.Items
            If (lt.Selected) Then
                DefaultTariff = "Y"
                If PORT = "" Then
                    PORT = lt.Value
                Else
                    PORT = "A"
                End If
            End If
        Next

        Dim MAT_Compute As String = radioCompute.SelectedValue
        Dim MAT_ComputeDestination As String = ""
        Dim MAT_COmputeBuy As String = ""
        Dim MAT_ComputeNational As String = ""
        Dim MAT_ComputebuyerSeller As String = ""
        Dim MAT_ComputeProduct As String = ""
        Dim ChkProductCompute As Boolean = False
        MAT_FOR = radioFor.SelectedValue
        MAT_ComputeDestination = radio_DestionationCompute.SelectedValue
        MAT_ComputeNational = radioNationalCompute.SelectedValue
        MAT_ComputebuyerSeller = radioBuyerSeller.SelectedValue
        If (MAT_ComputeDestination = "O") Then
            MAT_COmputeBuy = radioTypeBuyCompute.SelectedValue
        End If
        MAT_ComputeProduct = radioComputeProduct.SelectedValue
        If (radioComputeProduct.SelectedValue = "B") Then

            'ต้องทำการ Insert
            ChkProductCompute = True
            For i As Integer = 0 To gridComputeProduct1.Rows.Count - 1
                Dim chkList As CheckBoxList = CType(gridComputeProduct1.Rows(i).FindControl("chkAction"), CheckBoxList)
                For Each lt As ListItem In chkList.Items
                    If (lt.Selected) Then
                        Select Case lt.Text
                            Case "Load" : dc.Rows(i).Item("Product_Load") = "Y"
                            Case "Disch" : dc.Rows(i).Item("Product_Disch") = "Y"
                        End Select
                    Else
                        Select Case lt.Text
                            Case "Load" : dc.Rows(i).Item("Product_Load") = "N"
                            Case "Disch" : dc.Rows(i).Item("Product_Disch") = "N"
                        End Select
                    End If
                    dc.AcceptChanges()
                Next
            Next
            ViewState("ComputeProduct") = dc
        ElseIf (radioComputeProduct.SelectedValue = "C") Then
            'ต้องทำการ Insert
            ChkProductCompute = True
            For i As Integer = 0 To gridComputeProduct2.Rows.Count - 1
                Dim chkList As CheckBoxList = CType(gridComputeProduct2.Rows(i).FindControl("chkAction"), CheckBoxList)
                For Each lt As ListItem In chkList.Items
                    If (lt.Selected) Then
                        Select Case lt.Text
                            Case "Load" : dc.Rows(i).Item("Product_Load") = "Y"
                            Case "Disch" : dc.Rows(i).Item("Product_Disch") = "Y"
                        End Select
                    Else
                        Select Case lt.Text
                            Case "Load" : dc.Rows(i).Item("Product_Load") = "N"
                            Case "Disch" : dc.Rows(i).Item("Product_Disch") = "N"
                        End Select
                    End If
                    dc.AcceptChanges()
                Next
            Next
            ViewState("ComputeProduct") = dc
        End If
        Dim DWToperation As String = txtDWToperate.Text   'DWT > xxxxx ถึงทำงาน
        Dim Shifting As String = txtShifting.Text


        Dim dsRelation As DataSet = ViewState("Relation")
        If (dsRelation.Tables(0).Rows.Count > 0) Then
            If (dsRelation.Tables(0).Rows(0).Item("Name") & "" = "") Then
                MAT_Relation = "N"
            Else
                MAT_Relation = "Y"
            End If
        Else
            MAT_Relation = "N"
        End If

        tran = clsDB.BeginTran(conn)
        If (clsFc.UpdateMeterial(lbCode.Text, DefaultTariff, ddlWHT.SelectedValue, "", MAT_FOR, PORT, MAT_Relation, "", "", "", "", MAT_Compute, MAT_ComputeDestination, "", "", MAT_COmputeBuy, "", MAT_ComputebuyerSeller, MAT_ComputeNational, MAT_ComputeProduct, "", "", DWToperation, Shifting, ddlOperate.SelectedValue, conn, tran) > 0) Then

            If (clsFc.UpdateSubMeterial(lbCode.Text, ddlPer.SelectedValue, conn, tran) > 0) Then

                If (clsFc.DeleteRelationMATERIAL(lbCode.Text, conn, tran) > -1) Then
                    For Each dr As DataRow In dsRelation.Tables(0).Rows
                        If (dr.Item("Name") & "" <> "") And (dr.Item("Mat_Rate") & "" <> "") Then
                            If (clsFc.InsertRelationMATERIAL(MAT_CODE, lbCode.Text, dr.Item("Name").ToString(), dr.Item("Mat_Rate").ToString(), dr.Item("Relation").ToString(), dr.Item("Point_Start").ToString(), dr.Item("Point_End").ToString(), dr.Item("Money").ToString(), dr.Item("Point_Unit").ToString(), dr.Item("Vessel_National"), dr.Item("Vessel_Destination"), dr.Item("Data3"), dr.Item("Compute_Between"), dr.Item("Customer_no"), dr.Item("Customer_name"), dr.Item("Customer_Rate"), dr.Item("Customer_Money"), dr.Item("Compute_Count"), dr.Item("LowRate"), dr.Item("LowRate_Money"), dr.Item("Compute_C"), conn, tran) > 0) Then
                                MAT_CODE = (CInt(MAT_CODE) + 1).ToString("#####00000")
                            Else
                                condition = False
                            End If
                        End If

                    Next
                End If
                If (ChkProductCompute) Then
                    clsFc.DeleteMaterial_Product(lbCode.Text, "C", conn, tran)
                    For Each dr As DataRow In dc.Rows
                        If (dr.Item("Product_no") <> "") Then
                            If (clsFc.InsertMaterial_Product(lbCode.Text, dr.Item("Product_no"), dr.Item("Product_name"), dr.Item("Product_Load"), dr.Item("Product_Disch"), "C", conn, tran) < 1) Then
                                condition = False
                            End If
                        End If
                    Next
                End If
            Else
                'error
                condition = False
            End If
        Else
            'error
            condition = False
        End If
        If (condition) Then
            clsDB.CommitTran(tran)
            'Response.Write("<SCRIPT>alert('บันทึกข้อมูลสำเร็จ');</SCRIPT>")
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('บันทึกข้อมูลสำเร็จ'); parent.closeModal();", True)
        Else
            clsDB.RollBackTran(tran)
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('เกิดความผิดพลาดในการบันทึก');", True)
        End If
        Application.UnLock()
        conn.Dispose()
    End Sub

    Private Sub gridItem_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gridItem.RowCommand
        Dim ds As DataSet = ViewState("Relation")
        Dim Index As Integer = Convert.ToInt32(e.CommandArgument)
        If (e.CommandName = "cmdedit") Then
            ViewState("editcal") = Index
            txtNameCal.Text = ds.Tables(0).Rows(Index).Item("Name")
            If Not (ddlUnitCal.Items.FindByValue(ds.Tables(0).Rows(Index).Item("Relation")) Is Nothing) Then
                ddlUnitCal.ClearSelection()
                ddlUnitCal.SelectedValue = ds.Tables(0).Rows(Index).Item("Relation")
            End If
            Try
                If (CDbl(ds.Tables(0).Rows(Index).Item("Mat_Rate")) > 0.0) Then
                    RadioRate.Checked = True
                    RadioLOA.Checked = False
                    txtRate.Enabled = True
                    ddlRate.Enabled = True
                    txtRate.Text = ds.Tables(0).Rows(Index).Item("Mat_Rate")
                    If Not (ddlRate.Items.FindByValue(ds.Tables(0).Rows(Index).Item("Money")) Is Nothing) Then
                        ddlRate.ClearSelection()
                        ddlRate.SelectedValue = ds.Tables(0).Rows(Index).Item("Money")
                    End If
                End If

            Catch ex As Exception
                RadioLOA.Checked = True
                RadioRate.Checked = False
                txtRate.Enabled = False
                ddlRate.Enabled = False
                txtRate.Text = String.Empty
                ddlRate.ClearSelection()
            End Try
            txtLowRate.Text = ds.Tables(0).Rows(Index).Item("LowRate")
            If Not (ddlLowRate.Items.FindByValue(ds.Tables(0).Rows(Index).Item("LowRate_Money")) Is Nothing) Then
                ddlLowRate.ClearSelection()
                ddlLowRate.SelectedValue = ds.Tables(0).Rows(Index).Item("LowRate_Money")
            End If
            If (ds.Tables(0).Rows(Index).Item("Customer_no") & "" <> "") Then
                Dim Customer() As String = ds.Tables(0).Rows(Index).Item("Customer_no").ToString().Split(",")
                Dim Name() As String = ds.Tables(0).Rows(Index).Item("Customer_name").ToString().Split(",")
                Dim Rate() As String = ds.Tables(0).Rows(Index).Item("Customer_Rate").ToString().Split(",")
                Dim Money() As String = ds.Tables(0).Rows(Index).Item("Customer_Money").ToString().Split(",")
                Dim dt As DataTable = createCustomerDataTable()
                Try
                    For i As Integer = 0 To Customer.Length - 1
                        Dim dr As DataRow = dt.NewRow
                        dr.Item("Customer_no") = Customer(i).ToString()
                        dr.Item("Customer_name") = Name(i).ToString()
                        dr.Item("Customer_Rate") = Rate(i).ToString()
                        dr.Item("Customer_Money") = Money(i).ToString()
                        dt.Rows.Add(dr)
                        dt.AcceptChanges()
                    Next
                Catch ex As Exception

                End Try

                ViewState("dtCustomer") = dt
                gridCustomer.DataSource = dt
                gridCustomer.DataBind()
            End If
            txtStart.Text = ds.Tables(0).Rows(Index).Item("Point_Start")
            txtEnd.Text = ds.Tables(0).Rows(Index).Item("Point_End")
            txtPer.Text = ds.Tables(0).Rows(Index).Item("Point_Unit")
            If Not (radioNation.Items.FindByValue(ds.Tables(0).Rows(Index).Item("Vessel_National")) Is Nothing) Then
                radioNation.SelectedValue = ds.Tables(0).Rows(Index).Item("Vessel_National")
            End If
            If Not (radioDestination.Items.FindByValue(ds.Tables(0).Rows(Index).Item("Vessel_Destination")) Is Nothing) Then
                radioDestination.SelectedValue = ds.Tables(0).Rows(Index).Item("Vessel_Destination")
            End If
            If Not (radioData3.Items.FindByValue(ds.Tables(0).Rows(Index).Item("Data3")) Is Nothing) Then
                radioData3.SelectedValue = ds.Tables(0).Rows(Index).Item("Data3")
                If (radioData3.SelectedValue = "C") Then
                    rowCount.Visible = True
                    If (ds.Tables(0).Rows(Index).Item("Compute_Count") & "" = "") Then
                        If (ds.Tables(0).Rows(Index).Item("Compute_C") & "" = "C") Then
                            radioCount.SelectedValue = "C"
                        Else
                            radioCount.SelectedValue = "D"
                        End If
                        txtCountFix.Enabled = False
                        txtCountFix.Text = String.Empty
                    Else
                        radioCount.SelectedValue = "F"
                        txtCountFix.Enabled = True
                        txtCountFix.Text = ds.Tables(0).Rows(Index).Item("Compute_Count") & ""
                    End If
                Else
                    rowCount.Visible = False
                End If
            End If
            If Not (radioComputeBettween.Items.FindByValue(ds.Tables(0).Rows(Index).Item("Compute_Between")) Is Nothing) Then
                radioComputeBettween.SelectedValue = ds.Tables(0).Rows(Index).Item("Compute_Between")
            End If
        ElseIf (e.CommandName = "cmddelete") Then
            ds.Tables(0).Rows(Index).Delete()
            ds.AcceptChanges()
            If (ds.Tables(0).Rows.Count = 0) Then
                ds = CreateDataset()
            End If
            ViewState("Relation") = ds
            gridItem.DataSource = ViewState("Relation")
            gridItem.DataBind()
            clearTextRelation()
        End If
    End Sub

    Private Sub gridItem_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridItem.RowDataBound
        If (e.Row.RowType = DataControlRowType.DataRow) Then
            If (e.Row.Cells(0).Text.Trim() & "" = "") Or (e.Row.Cells(0).Text.Trim() & "" = "&nbsp;") Then
                Dim ImgEdit As ImageButton = CType(e.Row.FindControl("imgedit"), ImageButton)
                Dim ImgDelete As ImageButton = CType(e.Row.FindControl("imgdelete"), ImageButton)
                ImgEdit.Visible = False
                ImgDelete.Visible = False
            End If
        End If
    End Sub

    Private Sub radio_DestionationCompute_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles radio_DestionationCompute.SelectedIndexChanged
        If (radio_DestionationCompute.SelectedValue = "O") Then
            rowTypebuyCompute.Visible = True
        Else
            rowTypebuyCompute.Visible = False
        End If
    End Sub


    Private Sub addProduct(ByVal Product_name As String, ByVal Product_no As String)
        Dim dt As DataTable = ViewState("Product")
        If dt.Rows(0).Item("Product_name") & "" = "" Then
            dt.Rows(0).Item("Product_name") = Product_name
            dt.Rows(0).Item("Product_no") = Product_no
            dt.AcceptChanges()
        Else
            Dim dr As DataRow = dt.NewRow()
            dr.Item("Product_name") = Product_name
            dr.Item("Product_no") = Product_no
            dt.Rows.Add(dr)
            dt.AcceptChanges()
        End If
        ViewState("Product") = dt
    End Sub

    Private Sub addProductCompute(ByVal Product_name As String, ByVal Product_no As String)
        Dim dt As DataTable = ViewState("ComputeProduct")
        If dt.Rows(0).Item("Product_name") & "" = "" Then
            dt.Rows(0).Item("Product_name") = Product_name
            dt.Rows(0).Item("Product_no") = Product_no
            dt.AcceptChanges()
        Else
            Dim dr As DataRow = dt.NewRow()
            dr.Item("Product_name") = Product_name
            dr.Item("Product_no") = Product_no
            dt.Rows.Add(dr)
            dt.AcceptChanges()
        End If
        ViewState("ComputeProduct") = dt
    End Sub

    Public Function CreateDatasetProduct() As DataTable
        Dim ds As New DataSet
        Dim dt As New DataTable
        Dim dr As DataRow
        ds.Tables.Add(dt)
        dt.Columns.Add("Product_name", GetType(String))
        dt.Columns.Add("Product_no", GetType(String))
        dt.Columns.Add("Product_Load", GetType(String))
        dt.Columns.Add("Product_Disch", GetType(String))
        dr = ds.Tables(0).NewRow
        dr.Item("Product_name") = ""
        dr.Item("Product_no") = ""
        dr.Item("Product_Load") = ""
        dr.Item("Product_Disch") = ""
        ds.Tables(0).Rows.Add(dr)
        ds.Tables(0).AcceptChanges()
        Return ds.Tables(0)
    End Function

    Private Sub btnCustomer_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCustomer.Click
        Dim dt As DataTable = ViewState("dtCustomer")
        If ((clsGet.Get_Customer(txtid.Text.Trim().Split("-")(0))).Rows.Count < 1) Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('ไม่พบข้อมูลลูกค้า !!');", True)
            Exit Sub
        Else
            For Each drx As DataRow In dt.Rows
                If (drx.Item("Customer_no") & "" = txtid.Text.Trim().Split("-")(0)) Then
                    ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('พบข้อมูลลูกค้าอยู่ก่อนแล้ว !!');", True)
                    Exit Sub
                End If
            Next
        End If
        If (txtCustomerRate.Text.Trim() = ".") Or (txtCustomerRate.Text.Trim() = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุ Rate สำหรับลูกค้า !!');", True)
            Exit Sub
        End If
        Dim dr As DataRow = dt.NewRow()
        dr.Item("Customer_no") = txtid.Text.Split("-")(0)
        Dim NameCustomer As String = ""
        If (txtid.Text.Trim() <> "") Then
            Dim AgentLoops() As String = txtid.Text.Split("-")
            For i As Integer = 0 To AgentLoops.Length - 1
                If (i <> 0) Then
                    NameCustomer &= "-" & AgentLoops(i).ToString()
                End If
            Next
            NameCustomer = NameCustomer.Substring(1, NameCustomer.Length - 1)
        End If
        dr.Item("Customer_name") = NameCustomer
        dr.Item("Customer_Rate") = txtCustomerRate.Text
        dr.Item("Customer_Money") = ddlCustomerMoney.SelectedValue
        dt.Rows.Add(dr)
        dt.AcceptChanges()
        ViewState("dtCustomer") = dt
        gridCustomer.DataSource = dt
        gridCustomer.DataBind()
        txtid.Text = String.Empty
        txtCustomerRate.Text = String.Empty
    End Sub

    Private Function createCustomerDataTable() As DataTable
        Dim dt As New DataTable
        dt.Columns.Add("Customer_no", GetType(String))
        dt.Columns.Add("Customer_name", GetType(String))
        dt.Columns.Add("Customer_Rate", GetType(String))
        dt.Columns.Add("Customer_Money", GetType(String))
        dt.AcceptChanges()

        Return dt
    End Function

    Private Sub gridCustomer_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gridCustomer.RowCommand
        Dim Index As Integer = Convert.ToInt32(e.CommandArgument)
        If (e.CommandName = "cmddelete") Then
            Dim dt As DataTable = ViewState("dtCustomer")
            dt.Rows(Index).Delete()
            dt.AcceptChanges()
            ViewState("dtCustomer") = dt
            gridCustomer.DataSource = dt
            gridCustomer.DataBind()
        End If
    End Sub

    Private Sub gridCustomer_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridCustomer.RowDataBound
        If (e.Row.RowType = DataControlRowType.DataRow) Then
            If (gridCustomer.DataKeys(e.Row.RowIndex).Values("Customer_no") & "" <> "") Then
                e.Row.Cells(1).Text = gridCustomer.DataKeys(e.Row.RowIndex).Values("Customer_Rate") & " " & gridCustomer.DataKeys(e.Row.RowIndex).Values("Customer_Money")
            End If
        End If
    End Sub

    Private Sub RadioRate_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioRate.CheckedChanged
        If (RadioRate.Checked) Then
            txtRate.Enabled = True
            ddlRate.Enabled = True
            RadioLOA.Checked = False
        Else
            txtRate.Enabled = False
            ddlRate.Enabled = False
        End If
    End Sub

    Private Sub RadioLOA_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioLOA.CheckedChanged
        If (RadioLOA.Checked) Then
            txtRate.Enabled = False
            ddlRate.Enabled = False
            RadioRate.Checked = False
        End If
    End Sub

    Private Sub radioData3_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles radioData3.SelectedIndexChanged
        If (radioData3.SelectedValue = "C") Then
            rowCount.Visible = True
            radioCount.ClearSelection()
            radioCount.SelectedValue = "C"
            txtCountFix.Enabled = False
        Else
            rowCount.Visible = False
        End If
    End Sub

    Private Sub radioCount_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles radioCount.SelectedIndexChanged
        If (radioCount.SelectedValue = "F") Then
            txtCountFix.Enabled = True
        Else
            txtCountFix.Enabled = False
        End If
    End Sub

    Private Sub radioComputeProduct_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles radioComputeProduct.SelectedIndexChanged
        ViewState("ComputeProduct") = CreateDatasetProduct()
        If (radioComputeProduct.SelectedValue = "A") Then
            rowComputeProduct1.Visible = False
            rowComputeProduct2.Visible = False
        ElseIf (radioComputeProduct.SelectedValue = "B") Then
            rowComputeProduct1.Visible = True
            rowComputeProduct2.Visible = False
            ViewState("ComputeProduct") = CreateDatasetProduct()
            gridComputeProduct1.PageIndex = 0
            gridComputeProduct1.DataSource = ViewState("ComputeProduct")
            gridComputeProduct1.DataBind()
        Else
            rowComputeProduct2.Visible = True
            rowComputeProduct1.Visible = False
            ViewState("ComputeProduct") = CreateDatasetProduct()
            gridComputeProduct2.PageIndex = 0
            gridComputeProduct2.DataSource = ViewState("ComputeProduct")
            gridComputeProduct2.DataBind()
        End If
    End Sub

    Private Sub btnComputeProduct1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnComputeProduct1.Click
        Dim product_no As String = clsGet.Get_Group_no(txtComputeProduct1.Text)
        If product_no = "" Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('ไม่พบข้อมูลกลุ่มสินค้า !!');", True)
            Exit Sub
        End If
        Dim dt As DataTable = ViewState("ComputeProduct")
        For Each dr As DataRow In dt.Rows
            If (dr.Item("Product_no") & "" = product_no) Then
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('กลุ่มสินค้าได้ถูกเพิ่มก่อนหน้านี้แล้ว !!');", True)
                Exit Sub
            End If
        Next
        addProductCompute(txtComputeProduct1.Text, product_no)
        gridComputeProduct1.PageIndex = 0
        gridComputeProduct1.DataSource = ViewState("ComputeProduct")
        gridComputeProduct1.DataBind()
        txtComputeProduct1.Text = String.Empty
    End Sub

    Private Sub btnComputeProduct2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnComputeProduct2.Click
        Dim product_no As String = clsGet.Get_Group_no(txtComputeProduct2.Text)
        If product_no = "" Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('ไม่พบข้อมูลกลุ่มสินค้า !!');", True)
            Exit Sub
        End If
        Dim dt As DataTable = ViewState("ComputeProduct")
        For Each dr As DataRow In dt.Rows
            If (dr.Item("Product_no") & "" = product_no) Then
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('กลุ่มสินค้าได้ถูกเพิ่มก่อนหน้านี้แล้ว !!');", True)
                Exit Sub
            End If
        Next
        addProductCompute(txtComputeProduct2.Text, product_no)
        gridComputeProduct2.PageIndex = 0
        gridComputeProduct2.DataSource = ViewState("ComputeProduct")
        gridComputeProduct2.DataBind()
        txtComputeProduct2.Text = String.Empty
    End Sub

    Private Sub gridComputeProduct1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gridComputeProduct1.RowCommand
        Dim dt As DataTable = ViewState("ComputeProduct")
        Dim Index As Integer = Convert.ToInt32(e.CommandArgument)
        If (e.CommandName = "cmddelete") Then
            dt.Rows(Index).Delete()
            dt.AcceptChanges()
            ViewState("ComputeProduct") = dt
            gridComputeProduct1.PageIndex = 0
            gridComputeProduct1.DataSource = ViewState("ComputeProduct")
            gridComputeProduct1.DataBind()
        End If
    End Sub

    Private Sub gridComputeProduct2_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gridComputeProduct2.RowCommand
        Dim dt As DataTable = ViewState("ComputeProduct")
        Dim Index As Integer = Convert.ToInt32(e.CommandArgument)
        If (e.CommandName = "cmddelete") Then
            dt.Rows(Index).Delete()
            dt.AcceptChanges()
            ViewState("ComputeProduct") = dt
            gridComputeProduct2.PageIndex = 0
            gridComputeProduct2.DataSource = ViewState("ComputeProduct")
            gridComputeProduct2.DataBind()
        End If
    End Sub

    Private Sub gridComputeProduct1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridComputeProduct1.RowDataBound
        If (e.Row.RowType = DataControlRowType.DataRow) Then
            Dim chklist As CheckBoxList = CType(e.Row.FindControl("chkAction"), CheckBoxList)
            Dim imgdelete As ImageButton = CType(e.Row.FindControl("imgdelete"), ImageButton)
            If (gridComputeProduct1.DataKeys(e.Row.RowIndex).Values("Product_name") & "" = "") Then
                chklist.Visible = False
                imgdelete.Visible = False
            End If
        End If
    End Sub

    Private Sub gridComputeProduct2_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridComputeProduct2.RowDataBound
        If (e.Row.RowType = DataControlRowType.DataRow) Then
            Dim chklist As CheckBoxList = CType(e.Row.FindControl("chkAction"), CheckBoxList)
            Dim imgdelete As ImageButton = CType(e.Row.FindControl("imgdelete"), ImageButton)
            If (gridComputeProduct2.DataKeys(e.Row.RowIndex).Values("Product_name") & "" = "") Then
                chklist.Visible = False
                imgdelete.Visible = False
            End If
        End If
    End Sub

    Private Sub MainPage_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim ds As DataSet = Session("UserData")
        If Not (ds Is Nothing) Then
            Page.MasterPageFile = clsGet.GetMaster_Page(ds.Tables(0).Rows(0).Item("User_Name").ToString(), "")
        End If
    End Sub
End Class